//
// Copyright (c) 2024 BPer
//
#include <bperutil/BPerAvcType.h>
#include <bperutil/BPerAvInfoType.h>
#include <bperutil/BPerFrameBuffer.h>

void bPer_frame_copy_nobuffer( BPerFrame *src,  BPerFrame *dst) {
	if (!src || !dst)		return;
	dst->mediaType = src->mediaType;
	dst->uid = src->uid;
	dst->frametype = src->frametype;
	dst->nb = src->nb;
	dst->pts = src->pts;
	dst->dts=src->dts;
}

void bPer_frame_copy( BPerFrame *src,  BPerFrame *dst) {
	if (!src || !dst)		return;
	bPer_frame_copy_nobuffer(src,dst);
	dst->payload = src->payload;
}

void bPer_frame_copy_buffer( BPerFrame *src,  BPerFrame *dst) {
	if (!src || !dst)		return;
	bPer_frame_copy_nobuffer(src,dst);
	if (src->payload && dst->payload)
		bPer_memcpy(dst->payload, src->payload, src->nb);
}

void bPer_avinfo_initVideo(BPerVideoInfo *video) {
	video->width = 1280;
	video->height = 720;
	video->outWidth = 1280;
	video->outHeight = 720;
	video->rate = 2048;
	video->frame = 30;
	video->rotate=0;
	video->bitDepth = 8;

	video->videoCacheNum = 10;
	video->evideoCacheNum = 10;
	video->videoPlayCacheNum = 5;

	video->videoCaptureFormat = BPerYuy2;
	video->videoEncoderFormat = BPerI420;
	video->videoDecoderFormat = BPerI420;

	video->videoEncoderType = 0;
	video->videoDecoderType = 0;
	video->videoEncHwType = 0;
	video->videoDecHwType = 0;
	video->vIndex = 0;
}

void bPer_avinfo_initAudio(BPerAudioInfo *audio) {
	audio->sample = 48000;
	audio->frameSize = 960;
	audio->channel = 2;
	audio->bitrate = 128;
	audio->enableMono = BPerFalse;
	audio->enableAec = BPerFalse;
	audio->enableAudioFec = BPerTrue;
	audio->echoPath = 10;
	audio->aecBufferFrames = 0;
	audio->enableAudioHeader = BPerFalse;
	audio->audioEncoderType = 3;
	audio->audioDecoderType = 3;
	audio->audioPlayType = 0;
	audio->audioCacheNum = 10;
	audio->audioCacheSize = 10;
	audio->audioPlayCacheNum = 10;

	audio->aIndex = -1;
	audio->aSubIndex = 0;
}

void bPer_avinfo_initSys(BPerSysInfo *sys) {
	sys->familyType=BPer_IpFamilyType_IPV4;

	sys->enableLogFile = BPerFalse;
	sys->mediaServer=BPer_Server_Srs;
	sys->transType = BPer_Webrtc;
	sys->httpPort=1988;
	sys->logLevel = 1;
	bPer_strcpy(sys->whipUrl, "rtc/v1/whip/?app=%s&stream=%s");
	bPer_strcpy(sys->whepUrl, "rtc/v1/whip-play/?app=%s&stream=%s");
}

void bPer_avinfo_initEnc(BPerVideoEncInfo *enc) {
	enc->preset = 3;
	enc->level_idc = 31;
	enc->profile = 0;
	enc->keyint_max = 30;
	enc->enc_threads = 4;
	enc->gop=60;
	enc->createMeta=BPerTrue;
}

void bPer_avinfo_initRtc(BPerRtcInfo *rtc) {
	rtc->enableHttpServerSdp = BPerFalse;
	rtc->sessionTimeout= 30*1000000;

	rtc->iceCandidateType = 0;
	rtc->enableAudioBuffer = BPerFalse;

	rtc->iceUsingLocalIp=BPerFalse;
	rtc->iceServerPort=3478;

	rtc->rtcSocketProtocol=BPer_Socket_Protocol_Udp;
	rtc->turnSocketProtocol=BPer_Socket_Protocol_Udp;

	rtc->rtcPort = 1985;
	rtc->rtcLocalPort = 16000;

	bPer_strcpy(rtc->rtcServerIP, "127.0.0.1");
	bPer_strcpy(rtc->iceServerIP, "127.0.0.1");
	bPer_strcpy(rtc->iceLocalIP, "127.0.0.1");
	bPer_strcpy(rtc->iceUserName, "bperrtc");
	bPer_strcpy(rtc->icePassword, "bperrtc");
}



void bPer_init_avinfo(BPerAVInfo* avinfo){

	bPer_memset(&avinfo->video, 0, sizeof(BPerVideoInfo));
	bPer_memset(&avinfo->audio, 0, sizeof(BPerAudioInfo));
	bPer_memset(&avinfo->sys, 0, sizeof(BPerSysInfo));
	bPer_memset(&avinfo->enc, 0, sizeof(BPerVideoEncInfo));
	bPer_memset(&avinfo->rtc, 0, sizeof(BPerRtcInfo));

	bPer_avinfo_initAudio(&avinfo->audio);
	bPer_avinfo_initVideo(&avinfo->video);
	bPer_avinfo_initSys(&avinfo->sys);
	bPer_avinfo_initEnc(&avinfo->enc);
	bPer_avinfo_initRtc(&avinfo->rtc);

}
